


var ajax = require("../ajax/ajax");

var yz = require("../comm/yz");

var mongo = require("../mongo/mongo");

var validator = require("validator");
var moment = require("moment");
const task = require('../dict/task');

var mongoose = require('mongoose');
const py = require('../comm/py');

var fFlow = require("../flow/fFlow");
fCustomerOrder = require("./fCustomerOrder")
function get(req, res, r, c) {
    try {
        var para = req.body.para;


        switch (para) {
            case "saveCustomerOrder":  //保存订单详情信息

                saveCustomerOrder(req, res).then(r).catch(c);
                break;
            case "getCustomerOrder":   //获取订单详情
                getCustomerOrder(req, res).then(r).catch(c);
                break;
            case "getCustomerOrderList":   //获取订单信息列表
                getCustomerOrderList(req, res).then(r).catch(c);
                break;

            case "exeCustomerOrderFlow":   //执行工作流程
                exeCustomerOrderFlow(req, res).then(r).catch(c);
                break;

            case "getJd": //获取进度
                getJd(req, res).then(r).catch(c)
                break
            case "reItem": //获取进度
                reItem(req, res).then(r).catch(c)
                break
            case "setPrint": //获取进度
                setPrint(req, res).then(r).catch(c)
                break


            default:
        }
    } catch (e) {


        console.log(e);
        ajax.re(res, {

            re: "notok",
            err: e,
            errMsg: e
        })
    }


}

async function setPrint(req) {
    var j = req.body.j;
    if (!j._id) {
        throw '不能没有订单编号!'
    }


    var re = await mongo.upAsync({
        updateFind: {
            _id: mongoose.Types.ObjectId(j._id)
        },
        updateSet: {

            $set: { [j.flowClassId + ".print." + j.key]: j.val }
        },
        collection: "customerOrder",
        multi: false,
        upsert: false
    })
    return
}


async function reItem(req) {
    var j = req.body.j;

    if (!j.customerOrderId) {
        throw '不能没有订单编号!'
    }

    if (j.item.reAmount >= j.item.amount) {
        j.item.statusId = 300
    }
    else {
        j.item.statusId = 200
    }
    j.item.paymentTime = new Date()

    await mongo.upAsync({
        updateFind: {
            _id: mongoose.Types.ObjectId(j.customerOrderId),
            [j.item.flowClassId + ".t900.id"]: j.item.id
        },
        updateSet: {

            $set: {
                [j.item.flowClassId + ".t900.$"]: j.item
            }



        },
        collection: "customerOrder"
    })

    fCustomerOrder.count(j.customerOrderId)
    return { item: j.item }
}




async function getJd(req) {
    var j = req.body.j;

    if (!j._id) {
        throw '不能没有id'
    }
    if (yz.isArray(j.taskIdArray)) {

    }
    else {
        throw "必须传入数组taskIdArray"
    }


}


async function exeCustomerOrderFlow(req, res) {


    var j = req.body.j;
    var val = {};
    if (!j.customerOrderId) {
        throw '不能没有订单ID'
    }

    if (!j.flowClassId) {

        throw '不能没有flowClassId'
    }

    if (!j.nodeId) {
        throw '不能没有nodeId'

    }

    return await fFlow.exeNode(j.customerOrderId, j.flowClassId, j.nodeId)



}



//取得订单详情
async function getCustomerOrder(req, res) {
    var j = req.body.j;
    var val = {};
    if (!j._id) {
        throw '不能没有id'
    }


    var info = await mongo.findOneAsync({
        data: {
            _id: j._id
        },
        db: "uzor",
        collection: "customerOrder"
    })

    var customerInfo = null
    if (info.customerId) {
        //如果有客户信息,连客户信息一起返回

        customerInfo = await mongo.findOneAsync({

            data: {
                _id: info.customerId
            },
            db: "uzor",
            collection: "customer"

        })



    }
    var contractInfo = null;

    if (info.contractId) {
        contractInfo = await mongo.findOneAsync({

            data: {
                _id: info.contractId
            },
            db: "uzor",
            collection: "contract"

        })
    }



    return { info: info, customerInfo: customerInfo, contractInfo: contractInfo }



}




//保存用户订单
async function saveCustomerOrder(req, res) {

    var j = req.body.j;

    if (!validator.isLength(j.name, { min: 1, max: 100 })) {

        throw "名称长度必须在1到100之间"
    }
    j = yz.fmtCustomerOrder(j);

    if (!j.code) {
        j.code = moment().utcOffset(-8).format("YYMMDDHHmmssSSS");

    }

    if (!j._id || !j.createTime) {
        j.createTime = moment().utcOffset(-8)._d;
    }
    try {
        j.createTime = moment(j.createTime)._d;
    } catch (e) {

    } finally {

    }


    //首图?

    j.fileList.forEach(function (file) {
        if (file.isDefault == true) {
            j.imgUrl = file.url
            return false
        }

    }, this);


    if (!j._id) {
        //如果是新增的话,插入默认流程

        var proof = await mongo.findOneAsync({
            data: {
                isDefault: true,
                class: "proof"
            },
            collection: "Flow"
        })


        var bulk = await mongo.findOneAsync({
            data: {
                isDefault: true,
                class: "bulk"
            },
            collection: "Flow"
        })

        if (proof) {

            j.flow.proof.nodeList = proof.nodeList

        }

        if (bulk) {
            j.flow.bulk.nodeList = bulk.nodeList
        }




    }


    await mongo.SaveAsync({

        data: j,
        db: "uzor",
        collection: "customerOrder"


    });

    await fCustomerOrder.beginCustomerOrderFlow(j._id)


    return

}




//获取用户订单列表
async function getCustomerOrderList(req, res) {

    var j = req.body.j

    return await mongo.findAsync({

        data: j.data,
        limit: 10,
        db: "uzor",
        collection: "customerOrder",
        sort: { createTime: -1 },
        page: j.page


    })
}
module.exports = get;